package com.wenan.ui.controls.dropDownList
{
	
	import com.wenan.ui.controls.listBoxes.CheckBoxColumn;
	import com.wenan.ui.controls.listBoxes.CheckBoxHeaderRender;
	import com.wenan.ui.controls.listBoxes.CheckBoxItemRenderer;
	import com.wenan.ui.controls.listBoxes.CheckListBoxItemEvent;
	import com.wenan.ui.controls.listBoxes.DataGridRenderer;
	import com.wenan.ui.controls.listBoxes.DataGridSpecial;
	import com.wenan.ui.controls.listBoxes.RadioButtomItemRenderer;
	import com.wenan.ui.controls.listBoxes.SortRenderer;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import mx.collections.ArrayCollection;
	import mx.containers.Canvas;
	import mx.containers.HBox;
	import mx.containers.VBox;
	import mx.controls.Button;
	import mx.controls.DataGrid;
	import mx.controls.Image;
	import mx.controls.Label;
	import mx.controls.Spacer;
	import mx.controls.dataGridClasses.DataGridColumn;
	import mx.core.ClassFactory;
	import mx.events.CloseEvent;
	import mx.events.FlexEvent;
	import mx.events.ListEvent;
	
	[Event(name="itemSelected", type="com.wenan.ui.controls.listBoxes.CheckListBoxItemEvent")]
	public class SpecialBox extends VBox
	{
		private var _hasTitle:Boolean;
		private var _hasCloseButton:Boolean;
		private var _hasPageTool:Boolean = true;
		private var _type:String;
		private var titleLabel:Label;
		private var title:HBox;
		private var scroller:HBox;
		private var image:Image;
		private var closeButton:Button;
		public var dataGrid:DataGrid;
		[Bindable]
		private var dataTemp:ArrayCollection;
		private var _labelField:String = "name";
		private var _titleName:String;
		private var _titleIcon:Object;
		private var _isSort:Boolean = false;
		public var autoHeight:Boolean = true;
		public var columnRenderer:Boolean = false;
		public var closeWindowFunction:Function;
		
		public var pageSize:int;
		private var pageno:int = 1;
		private var pageTotle:int;
		private var first:int;
		private var last:int;
		private var dataTotle:int;
		
		public function SpecialBox()
		{
			super();
			
			setStyle("verticalGap", "0");
			setStyle("borderStyle", "none");
			this.horizontalScrollPolicy = "off";
			this.verticalScrollPolicy = "off";
		}
		
		public function get titleIcon():Object
		{
			return _titleIcon;
		}
		
		public function set titleIcon(value:Object):void
		{
			_titleIcon = value;
		}
		
		public function set hasPageTool(value:Boolean):void
		{
			_hasPageTool = value;
		}
		
		public function get hasPageTool():Boolean
		{
			return _hasPageTool;
		}
		
		public function get type():String
		{
			return _type;
		}
		
		public function set type(value:String):void
		{
			_type = value;
		}
		
		public function get hasCloseButton():Boolean
		{
			return _hasCloseButton;
		}
		
		public function get hasTitle():Boolean
		{
			return _hasTitle;
		}
		
		public function set hasCloseButton(value:Boolean):void
		{
			_hasCloseButton = value;
		}
		
		public function set hasTitle(value:Boolean):void
		{
			_hasTitle = value;
		}
		
		public function get titleName():String
		{
			return _titleName;
		}
		
		public function set titleName(value:String):void
		{
			_titleName = value;
		}
		
		public function get isSort():Boolean
		{
			return _isSort;
		}
		
		public function set isSort(value:Boolean):void
		{
			_isSort = value;
		}
		
		[Bindable]
		public function get dataProvider():Object
		{
			if (dataGrid)
				return dataGrid.dataProvider;
			return null;
		}
		
		public function set dataProvider(value:Object):void
		{
			dataTemp = value as ArrayCollection;
			dataTotle = dataTemp.length;
			if (dataGrid)
			{
//				dataGrid.dataProvider = value;
				dataGrid.invalidateProperties();
				dataGrid.invalidateSize();
				dataGrid.invalidateDisplayList();
			}
		}
		
		public function get labelField():String
		{
			return this._labelField;
		}
		
		public function set labelField(value:String):void
		{
			this._labelField = value;
		}
		
		public function get selectedItem():Object
		{
			return dataGrid.selectedItem;
		}
		
		public function set selectedItem(value:Object):void
		{
			dataGrid.selectedItem = value;
		}
		
		public function get selectedItems():Array
		{
			return dataGrid.selectedItems;
		}
		
		public function set selectedItems(value:Array):void
		{
			dataGrid.selectedItems = value;
		}
		
		/**
		 * 初始化控件。
		 */
		override protected function createChildren():void
		{
			super.createChildren();
			if (_hasTitle || _hasCloseButton)
			{
				title = new HBox();
				title.percentWidth = 100;
				title.height = 24;
				title.setStyle("verticalAlign", "middle");
				title.setStyle("paddingLeft", "8");
				title.styleName = "RepeatBackGround";
				
				image = new Image();
				if (titleIcon != null)
				{
					title.addChild(image);	
				}
				titleLabel = new Label();
				titleLabel.percentWidth = 100;
				title.addChild(titleLabel);
				
				
				if (_hasCloseButton)
				{
					closeButton = new Button();
					closeButton.styleName = "close";
					closeButton.buttonMode = true;
					closeButton.useHandCursor = true;
					closeButton.addEventListener(MouseEvent.CLICK, closeWindowHandler);
					var sp:Spacer = new Spacer();
					sp.width = 3;
					title.addChild(closeButton);
					title.addChild(sp);
				}
				addChild(title);
			}
			
			
			var client:Canvas = new Canvas();
			client.percentWidth = 100;
			client.percentHeight = 100;
			client.styleName = "GradientColor";
			client.horizontalScrollPolicy = "off";
			client.verticalScrollPolicy = "off";
			
			var clientBox:VBox = new VBox();
			clientBox.percentWidth = 100;
			clientBox.percentHeight = 100;
			clientBox.setStyle("verticalGap", "0");
			clientBox.horizontalScrollPolicy = "off";
			clientBox.verticalScrollPolicy = "off";
			initilizeClientBox(clientBox);
			
			client.addChild(clientBox);
			
			addChild(client);
		}
		
		private function initilizeClientBox(clientBox:VBox):void
		{
			if (type != null)
			{
				dataGrid = new DataGridSpecial();
				dataGrid.addEventListener(CheckListBoxItemEvent.ITEM_SELECTED, itemSelectedHandler);
			} else 
			{
				dataGrid = new DataGrid();
				dataGrid.addEventListener(ListEvent.CHANGE, dataGridChangeHandler);
			}
			dataGrid.setStyle("verticalAlign", "middle");
			dataGrid.setStyle("borderStyle","none");
			dataGrid.top=0;
			dataGrid.percentWidth = 100;
			dataGrid.percentHeight = 100;
			dataGrid.rowHeight=22;
			dataGrid.showHeaders = false;
			dataGrid.verticalScrollPolicy = "off";
			
			clientBox.addChild(dataGrid);
			
			if (_hasPageTool)
			{
				scroller = new HBox();
				scroller.percentWidth = 100;
				scroller.height = 18;
				scroller.setStyle("verticalAlign", "middle");
				scroller.setStyle("horizontalAlign", "center");
				
				var upButton:Button = new Button();
				upButton.styleName = "UpButton";
				upButton.buttonMode=true;
				upButton.addEventListener(MouseEvent.CLICK, upClickHander);
				scroller.addChild(upButton);
				
				var downButton:Button = new Button();
				downButton.styleName = "DownButton";
				downButton.buttonMode=true;
				downButton.addEventListener(MouseEvent.CLICK, downClickHander);
				scroller.addChild(downButton);
				
				clientBox.addChild(scroller);
			}
		}
		
		override protected function initializationComplete():void
		{
			super.initializationComplete();
			dataGrid.columns = buildDataGridColumns();
			
		}
		
		private function buildDataGridColumns():Array
		{
			var columns:Array = [];
			
			if (type != null)
			{
				var checkColumn:CheckBoxColumn = new CheckBoxColumn();
				checkColumn.width = 25;
				checkColumn.sortable = false;
				checkColumn.headerRenderer = new ClassFactory(CheckBoxHeaderRender);
				if (type == "checkBox")
				{
					checkColumn.itemRenderer = new ClassFactory(CheckBoxItemRenderer);	
				} else 
				{
					checkColumn.itemRenderer = new ClassFactory(RadioButtomItemRenderer);
				}
				columns.push(checkColumn);
			} 
		
			dataGrid.dataProvider = dataTemp;
			
			var nameColumn:DataGridColumn = new DataGridColumn();
			nameColumn.dataField = labelField;
			if (columnRenderer)
			{
				dataGrid.setStyle("useRollOver", false);
				dataGrid.setStyle("selectionColor", 0xF1F5FB);
				nameColumn.itemRenderer = new ClassFactory(DataGridRenderer)
			}
			columns.push(nameColumn);
			
			if (isSort)
			{
				var dgc:DataGridColumn = new DataGridColumn();
				dgc.width = 25;
				dgc.itemRenderer = new ClassFactory(SortRenderer);
				columns.push(dgc);
			}
			return columns;
		}
		
		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
		{
			var tempHeight:Number = unscaledHeight;
			if (title)
			{
				tempHeight = tempHeight - title.height;
			}
			tempHeight = tempHeight - scroller.height;
			var h:Number;
			if (pageSize >= dataTotle)
			{
				h = dataTotle * dataGrid.rowHeight;// this.dataGrid.minHeight
				if (title)
				{
					unscaledHeight = h + title.height;
				}
				scroller.height = 0;
			} else 
			{
				h = pageSize * dataGrid.rowHeight;//this.dataGrid.minHeight
				if (title)
				{
					unscaledHeight = h + title.height;
				}
				unscaledHeight = unscaledHeight + scroller.height;
			}
			super.updateDisplayList(unscaledWidth, unscaledHeight);
			if (autoHeight)
			{
				if (this.height < unscaledHeight)
				{
					this.parent.y = this.parent.y - (unscaledHeight - this.height);
				}
				this.height = unscaledHeight;
			}
		}
		
		override protected function commitProperties():void
		{
			if (titleLabel)
			{
				titleLabel.text = titleName;
			}
			if (image)
			{
				image.source = titleIcon;
			}
		}
		
		private function itemSelectedHandler(event:Event):void 
		{
			var obj:Object = event;
			dispatchEvent(new CheckListBoxItemEvent(CheckListBoxItemEvent.ITEM_SELECTED, obj.item));
		}
		
		private function dataGridChangeHandler(event:ListEvent):void 
		{
			dispatchEvent(new ItemEvent(ItemEvent.ITEM_SELECTED, event));
		}
		private function dataGridClickHandler(event:MouseEvent):void 
		{
			dispatchEvent(new ItemEvent(ItemEvent.ITEM_CLICK, event));
		}
		
		private function upClickHander(event:MouseEvent):void
		{
			if (pageno > 1) {
				pageno--;
				changeDataProvider();
			}
		}
		
		private function downClickHander(event:MouseEvent):void
		{
			pageTotle = (dataTotle + pageSize - 1) / pageSize;
			if (pageno < pageTotle) {
				pageno++;
				changeDataProvider();
			}
		}
		
		private function changeDataProvider():void 
		{
			first = (pageno - 1) * pageSize;
			last = pageno * pageSize -1;
			var temp:ArrayCollection = new ArrayCollection();
			for (var i:int=first; i <dataTotle && i<=last; i++){
				temp.addItem(dataTemp.getItemAt(i));
			}
			dataGrid.dataProvider = temp;
		}
		
		private function closeWindowHandler(event:MouseEvent):void 
		{
			closeWindowFunction();
		}
		
		public function invalidateList():void{
			if (dataGrid)
			{
				dataGrid.invalidateList();
			}
		}
	}
}